package com.example.tnot.Teacher.MinePart.container;

import android.os.Bundle;

import androidx.activity.OnBackPressedCallback;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.example.tnot.R;
import com.example.tnot.Teacher.ActivityConfig.TeacherHomeActivity;
import com.example.tnot.Teacher.MinePart.itembean.rv_SetListItem;

import java.util.List;

public class MeFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private RecyclerView rv_MainPager;
    private LinearLayout ll_personal;
    private List<rv_SetListItem> data;
    public NavController navController;
    protected OnBackPressedCallback onBackPressedCallback;

    private String mParam1;
    private String mParam2;

    public MeFragment() {
    }
    public static MeFragment newInstance(String param1, String param2) {
        MeFragment fragment = new MeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view = inflater.inflate(R.layout.teacher_fragment_me, container, false);
        // 获取当前Fragment中的NavController（如果有）
        Fragment navHostFragment = getChildFragmentManager().findFragmentById(R.id.teaHome_nav_host_fragment);
        if (navHostFragment instanceof NavHostFragment) {
            navController = ((NavHostFragment) navHostFragment).getNavController();
        }

        // 创建并注册返回回调
        onBackPressedCallback = new OnBackPressedCallback(true) {
            @Override
            public void handleOnBackPressed() {
                handleBackPressed();
            }
        };
        requireActivity().getOnBackPressedDispatcher().addCallback(getActivity(), onBackPressedCallback);

       return view;
    }

    protected void handleBackPressed() {
        // 优先使用Navigation组件的返回栈
        // 确保 navController 已正确初始化
        if (navController != null && navController.getCurrentDestination() != null) {
            Log.d("NavController", navController.toString());
            Log.d("NavControllerDestination", navController.getCurrentDestination().toString());
            if (!navController.popBackStack()) {
                // Navigation返回栈为空，执行通用逻辑
                performCommonCleanup();

                // 禁用当前回调，让Activity级别的回调处理
                onBackPressedCallback.setEnabled(false);
                requireActivity().onBackPressed();
            }
        } else {
            // 没有Navigation组件，使用原来的逻辑
            if (getParentFragmentManager().getBackStackEntryCount() > 0) {
                getParentFragmentManager().popBackStack();
            } else {
                performCommonCleanup();
                onBackPressedCallback.setEnabled(false);
                requireActivity().onBackPressed();
            }
        }
    }
    private void performCommonCleanup() {
        if (getActivity() instanceof TeacherHomeActivity) {
            ((TeacherHomeActivity) getActivity()).showBottomNavigation();
            ((TeacherHomeActivity) getActivity()).enableViewPagerSwipe();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        // 移除回调，避免内存泄漏
        if (onBackPressedCallback != null) {
            onBackPressedCallback.remove();
        }
    }
}